package io.xccit.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.xccit.common.result.AjaxResult;
import io.xccit.log.annotation.Log;
import io.xccit.log.enums.BusinessType;
import io.xccit.model.system.SysMenu;
import io.xccit.model.system.SysRole;
import io.xccit.model.vo.AssginMenuVo;
import io.xccit.model.vo.AssginRoleVo;
import io.xccit.model.vo.SysRoleQueryVo;
import io.xccit.system.service.ISysMenuService;
import io.xccit.system.service.ISysRoleMenuService;
import io.xccit.system.service.ISysRoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author CH_ywx
 * @date 2023-06-22
 * @description
 */
@Api(tags = "角色接口")
@RestController
@CrossOrigin
@RequestMapping("/api/system/sysRole")
public class SysRoleController {

    @Resource
    private ISysRoleService sysRoleService;

    @Resource
    private ISysMenuService sysMenuService;

    @Resource
    private ISysRoleMenuService sysRoleMenuService;

    @Log(title = "角色列表")
    @PreAuthorize("hasAuthority('btn.sysRole.list')")
    @ApiOperation("角色列表")
    @GetMapping("/list")
    public AjaxResult<List<SysRole>> list() {
        return AjaxResult.ok(sysRoleService.list());
    }


    @Log(title = "删除角色",businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('btn.sysRole.remove')")
    @ApiOperation("删除角色")
    @DeleteMapping("/remove/{id}")
    public AjaxResult<Boolean> removeByID(@PathVariable Long id){
        return AjaxResult.ok(sysRoleService.removeById(id));
    }

    /**
     * 条件分页查询,mapper中使用原生mybatis的方式实现
     * @param pageNo
     * @param pageSize
     * @param sysRoleQueryVo
     * @return
     */
    @Log(title = "角色获取")
    @PreAuthorize("hasAuthority('btn.sysRole.list')")
    @ApiOperation("条件分页查询")
    @GetMapping("/page/{pageNo}/{pageSize}")
    public AjaxResult<IPage<SysRole>> selectPage(@ApiParam(value = "当前页",required = true) @PathVariable Long pageNo,
                                                 @ApiParam(value = "每页条数",required = true) @PathVariable Long pageSize,
                                                 @ApiParam("角色条件查询对象") SysRoleQueryVo sysRoleQueryVo){
        Page<SysRole> sysRolePage = new Page<>(pageNo, pageSize);
        IPage<SysRole> pageModel = sysRoleService.selectPage(sysRolePage,sysRoleQueryVo);
        return AjaxResult.ok(pageModel);
    }

    @Log(title = "角色添加",businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('btn.sysRole.add')")
    @ApiOperation("添加角色")
    @PostMapping("/save")
    public AjaxResult<String> saveRole(@ApiParam(value = "添加角色",required = true) @RequestBody SysRole sysRole){
        sysRoleService.save(sysRole);
        return AjaxResult.ok("添加成功");
    }

    @Log(title = "角色修改",businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('btn.sysRole.update')")
    @ApiOperation("修改角色")
    @PutMapping("/update")
    public AjaxResult<String> updateRole(@ApiParam(value = "修改的角色",required = true) @RequestBody SysRole sysRole){
        sysRoleService.updateById(sysRole);
        return AjaxResult.ok("修改成功");
    }

    @Log(title = "批量删除角色",businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('btn.sysRole.remove')")
    @ApiOperation("批量删除角色")
    @DeleteMapping("/removeBatch")
    public AjaxResult<String> removeBatch(@ApiParam(value = "角色ID列表",required = true)@RequestBody List<Long> ids){
        sysRoleService.removeByIds(ids);
        return AjaxResult.ok("删除成功");
    }

    @Log(title = "角色菜单列表获取")
    @PreAuthorize("hasAuthority('btn.sysRole.assignAuth')")
    @ApiOperation("获取角色拥有的菜单及菜单列表")
    @GetMapping("/toAssign/{roleID}")
    public AjaxResult<List<SysMenu>> toAssign(@PathVariable Long roleID){
        List<SysMenu> sysMenuList = sysMenuService.selectMenuByRoleID(roleID);
        return AjaxResult.ok(sysMenuList);
    }

    @Log(title = "角色分配菜单")
    @PreAuthorize("hasAuthority('btn.sysRole.assignAuth')")
    @ApiOperation("分配菜单/权限")
    @PostMapping("/doAssign")
    public AjaxResult<String> doAssign(@RequestBody AssginMenuVo assginMenuVo){
        boolean saved = sysRoleMenuService.doAssgin(assginMenuVo);
        if (saved){
            return AjaxResult.ok("设置成功");
        }else{
            return AjaxResult.fail("设置失败");
        }
    }
}
